import { request } from "@/utils/service"
// import type * as Login from "./types/login"

/** 登录接口 */
export function getTokenApi(username, password) {
  return request({
    url: `/login/getToken?username=${username}&password=${password}`,
    method: "get"
  }).then((response) => {
    // 假设 response.data.token 是返回的 token
    const token = response.data.token
    if (token) {
      localStorage.setItem("authToken", token) // 将 token 保存到 localStorage
    }
    return response // 返回响应
  })
}

export function getCoordinateApi(page, size) {
  const token = localStorage.getItem("authToken")
  return request({
    url: `/vehicle/page/?page=${page}&size=${size}`,
    method: "get",
    headers: {
      Authorization: `Bearer ${token}`,
      "Content-Type": "application/json" // 根据API要求添加
    }
  })
}

// http://1.95.39.208:8085/api/v1/vehicle/last 获取最新轨迹点
export function getVehicleLast() {
  const token = localStorage.getItem("authToken")
  return request({
    url: `vehicle/last`,
    method: "get",
    headers: {
      Authorization: `Bearer ${token}`,
      "Content-Type": "application/json" // 根据API要求添加
    }
  })
}

// http://1.95.39.208:8085/api/v1/area/save  保存电子围栏
export function getAreaSave(data: { type: string; latitude: string; longitude: string; radius: number }) {
  const token = localStorage.getItem("authToken")
  console.log(token, "token")

  return request({
    url: `area/save`,
    method: "post",
    headers: {
      Authorization: `Bearer ${token}`,
      "Content-Type": "application/json" // 根据API要求添加
    },
    data: JSON.stringify(data) // 将数据转为 JSON 字符串
  })
}

// http://1.95.39.208:8085/api/v1/area/page/?page=1&size=200 电子围栏分页查询

export function getAreaPage(page, size) {
  const token = localStorage.getItem("authToken")
  return request({
    url: `area/page?page=${page}&size=${size}`,
    method: "get",
    headers: {
      Authorization: `Bearer ${token}`,
      "Content-Type": "application/json" // 根据API要求添加
    }
  })
}
